package org.geektimes.projects.user.web.controller;

import org.eclipse.microprofile.config.Config;
import org.geektimes.projects.user.web.RequestConfigHolder;
import org.geektimes.web.mvc.controller.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;

/**
 * @author karidyang
 */
@Path("/test")
public class TestRestController implements RestController {

    @GET
    @Path("/foo")
    public String foo(String foo, HttpServletRequest request) {
        System.out.println("org.geektimes.projects.user.web.controller.TestRestController.foo");
        System.out.println("foo=" + foo);
        Config config = RequestConfigHolder.getInstance().getConfig();
        String applicationName = config.getValue("application.name", String.class);
        return String.format("foo=%s, applicationName=%s", foo, applicationName);
    }


    @POST
    @Path("/bar")
    public String bar(HttpServletRequest request) throws IOException {
        StringBuilder sb = new StringBuilder();
        try(ServletInputStream inputStream = request.getInputStream();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
            String line = bufferedReader.readLine();
            if (line != null) {
                System.out.println(line);
                sb.append(line);
            }
        }
        return sb.toString();
    }

    @POST
    @Path("/bar2")
    public String bar(String bar) throws IOException {
        return "hello " + bar;
    }


}
